package cn.itlb.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
//处理文件上传的类
@WebServlet("/fileServlet")
@MultipartConfig
public class Fileon extends HttpServlet{
	Log log = LogFactory.getLog(this.getClass());
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		Part file1= request.getPart("file");//取文件域
//
		String name=request.getParameter("name");
		log.info(file1.getName());
		log.info(name);
		log.info(file1.getSubmittedFileName());
		File file =new File("d:/"+file1.getSubmittedFileName());//Tomcat 8以上才有这个.getSubmittedFileName()属性
		FileOutputStream fos= new FileOutputStream(file);
		IOUtils.copy(file1.getInputStream(), fos);
		fos.close();//必须关闭,否则拷贝不过去内容，只是名字		
	}
	
}
